/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.business.account.manage.repository.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.business.account.core.domain.account.AccountLog;
import info.peigen.hotpot.business.account.core.repository.entity.AccountLogEntity;
import info.peigen.hotpot.business.account.manage.convertor.AccountLogManageConvertor;
import info.peigen.hotpot.business.account.manage.repository.mapper.AccountLogManageMapper;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlRepository;
import org.noear.solon.annotation.Component;

/**
 * <b>(AccountLogManageRepository)</b>
 * 管理后台 "账户流水记录" 仓储服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023-08-29
 */
@SuppressWarnings("unused")
@Component
public class AccountLogManageRepository extends AbstractBeetlSqlRepository<AccountLogManageMapper, AccountLogEntity> {

    public AccountLog active(String accountLogId) {
        Assert.isTrue(StrUtil.isNotEmpty(accountLogId), () -> new BusinessException(ResultCode.FAILURE, "激活时[accountLogId]不得为空!"));

        AccountLogEntity entity = getMapper().findByAccountLogId(accountLogId);

        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "激活失败!accountLogId=" + accountLogId));

        return AccountLogManageConvertor.convertToDomain(entity);
    }

    public AccountLog lock(String accountLogId) {
        Assert.isTrue(StrUtil.isNotEmpty(accountLogId), () -> new BusinessException(ResultCode.FAILURE, "加锁时[accountLogId]不得为空!"));

        AccountLogEntity entity = getMapper().lockByAccountLogId(accountLogId);

        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "锁定失败!accountLogId为空!"));

        return AccountLogManageConvertor.convertToDomain(entity);
    }

    public void removeByAccountLogId(String accountLogId) {
        Assert.isTrue(StrUtil.isNotEmpty(accountLogId), () -> new BusinessException(ResultCode.FAILURE, "删除时[accountLogId]不得为空!"));

        getMapper().deleteByAccountLogId(accountLogId);
    }

    public void store(AccountLog domain) {
        Assert.isTrue(ObjectUtil.isNotEmpty(domain), () -> new BusinessException(ResultCode.FAILURE, "持久化时[domain]不得为空!"));

        AccountLogEntity entity = AccountLogManageConvertor.convertToEntity(domain);

        saveOrUpdate(entity);
    }
}